package com.fanjia.crm.service.impl;

import com.fanjia.crm.mapper.PermissionMapper;
import com.fanjia.crm.model.Permission;
import com.fanjia.crm.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by FanJia on 2020/9/19 21:25
 */
@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    PermissionMapper permissionMapper;


    @Override
    public List<Permission> findAllMenus() {
        return permissionMapper.findAllMenus();
    }

    @Override
    public List<Permission> findAllPermission() {
        return permissionMapper.findAllPermission();
    }

    @Override
    public List<Permission> findAllParentMenus() {
        return permissionMapper.findAllParentMenus();
    }

    @Override
    public void insert(Permission permission) {
        if (permission.getParent_id() == 0) {
            permission.setAuthorization_flag(null);
            permission.setPath(null);
        } else {
            String[] strArr = permission.getPath().split("/");
            String af = permission.getAuthorization_flag() + "_" + strArr[1];
            permission.setAuthorization_flag(af.toUpperCase());
        }


        if (permission.getParent_id()==0){
            int maxZIndex = permissionMapper.findMaxZIndex() + 1;
            permission.setZ_index(maxZIndex);

        }else {
            Permission parentNode = permissionMapper.findPermissionById(permission.getParent_id());
            permission.setZ_index(parentNode.getZ_index());
        }

        System.out.println("插入的数据：" + permission);
        permissionMapper.insert(permission);
    }

    @Override
    public List<Permission> findPermissionListByUserId(Integer userId) {
        return permissionMapper.findPermissionByUserId(userId);
    }


}
